// Copyright (C) 2025 woodycpp
//
// This work is licensed under the Creative Commons Attribution-ShareAlike 4.0
// International License. To view a copy of this license, visit
// https://creativecommons.org/licenses/by-sa/4.0/
//
// You are free to:
//   * Share — copy and redistribute the material in any medium or format
//   * Adapt — remix, transform, and build upon the material
//
// Under the following terms:
//   * Attribution — You must give appropriate credit, provide a link to the
//   license,
//     and indicate if changes were made. You may do so in any reasonable
//     manner, but not in any way that suggests the licensor endorses you or
//     your use.
//   * ShareAlike — If you remix, transform, or build upon the material, you
//   must
//     distribute your contributions under the same license as the original.
//
// In short: You must credit the original author (woodycpp, 2025) and share any
// derivative works under the same CC-BY-SA-4.0 license.

#include <stdio.h>

#include "print_last_kth.h"

int printLastKth(LinkList list, int k) {
    LinkList fast = list;
    // 快指针先向后移动k次
    for (int i = 0; i < k && fast; ++i) {
        fast = fast->link;
    }
    if (!fast) {
        return 0;  // 查找失败，返回0
    }
    LinkList slow = list;
    while (fast) {
        // 双指针同步移动
        fast = fast->link;
        slow = slow->link;
    }
    // 当快指针到达末尾时，慢指针刚好指向倒数第k个结点，打印data
    printf("%d", slow->data);
    return 1;
}